package forum.service;

import forum.DBConnectie;
import forum.Filter;
import forum.Variable;
import forum.wrapper.WrapRequest;

import javax.servlet.http.HttpSession;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.sql.ResultSet;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Ivan
 * Date: 2010-7-8
 * Time: 15:03:12
 */
public class User {
    DBConnectie db = new DBConnectie(Variable.getDb(), Variable.getDbLogin(), Variable.getDbPassword());

    public String add(WrapRequest request) throws Exception{

            String RegUser = request.getParameter("user");
            String RegEmail = request.getParameter("email");
            String RegPass = request.getParameter("password");
            String RegPass2 = request.getParameter("password2");

            db.connect();
            ResultSet rs = db.selectQuery(
                    "SELECT * " +
                            "FROM forum_users " +
                            "WHERE user_name=\"" + RegUser + "\"");

            String DBUsername = null;
            while (rs.next()) {
                DBUsername = rs.getString("user_name");
            }

            if (DBUsername == null) {

                if (RegPass.equals(RegPass2)) {

                    db.query(
                            "INSERT INTO " +
                                    "forum_users(user_name,password,email,type,avatar,member_title) " +
                                    "VALUES(\"" + RegUser + "\",password(\"" + RegPass + "\"),\"" + RegEmail + "\",\"user\",\"\",\"\")");
                    db.close();
                    return  "info.jsp?action=regcomplete";
                } else {
                    return  "info.jsp?action=wrongpass";
                }
            } else {
                return "info.jsp?action=userexists";
            }
    }

    public String edit(WrapRequest request) throws Exception{

            HttpSession session = request.getSession(true);
            String sessionUsername = (String) session.getAttribute("username");
            String sessionType = (String) session.getAttribute("type");

            String avatar_list = request.getParameter("avatar_list");
            String link_avatar = request.getParameter("link_avatar");
            String member_title = request.getParameter("member_title");
            String signature = request.getParameter("signature");

            member_title = Filter.filterAll(member_title);
            signature = Filter.filterAll(signature);

            String avatar = null;
            if (link_avatar.equals("http://")) {
                avatar = avatar_list;
            } else {
                avatar = link_avatar;
            }

            db.connect();

//                    db.query("INSERT INTO forum_users(user_name) VALUES(\"" + sessionUsername + "\"");
            db.query("UPDATE forum_users SET type = \"" + sessionType + "\" WHERE user_name =\"" + sessionUsername + "\"");
            db.query("UPDATE forum_users SET avatar = \"" + avatar + "\" WHERE user_name =\"" + sessionUsername + "\"");
            db.query("UPDATE forum_users SET member_title = \"" + member_title + "\" WHERE user_name =\"" + sessionUsername + "\"");
            db.query("UPDATE forum_users SET signature = \"" + signature + "\" WHERE user_name =\"" + sessionUsername + "\"");

            db.close();

            return  "index.jsp?page=profile";
    }

     public String login(WrapRequest request) throws Exception {

        String loginUser = request.getParameter("username");
        String loginPass = request.getParameter("password");
        
        
        List<File> l = request.getUploadFiles();
        
        for(int i = 0; i < l.size();i++){
        	System.out.println("Upload Files");
        	File f = l.get(i);
        	FileInputStream fis = new FileInputStream(f);
        	File fo = new File("D:\\f" + i + ".txt");
        	System.out.println(fo.getPath());
            FileOutputStream fos = new FileOutputStream(fo);
            
            byte[] b = new byte[512];
            int s = 0;
            while((s = fis.read(b))!=-1){
            	fos.write(b, 0, s);
            }
            fos.close();
            fis.close();
        }
        
        
        
        

        db.connect();

        String password = "null";
        String password2 = "zero";
        String type = "null";

        ResultSet rs = db.selectQuery(
                "SELECT * " +
                        "FROM forum_users " +
                        "WHERE user_name =\"" + loginUser + "\"");
        while (rs.next()) {
            password = rs.getString("password");
            type = rs.getString("type");
        }

        ResultSet rs2 = db.selectQuery(
                "SELECT " + "password(\"" + loginPass + "\") password");
        while (rs2.next()) {
            password2 = rs2.getString("password");
        }

        String return_str = "";

        if (password2.equals(password)) {
            HttpSession session = request.getSession(true);

            session.setAttribute("username", loginUser);
            session.setAttribute("password", password);
            session.setAttribute("type", type);
            
            if(session.getAttribute("history_url")!=null){
            	return_str = (String) session.getAttribute("history_url");
            }else{
            	return_str = "index.jsp";
            }
            
        } else {
            return_str = "info.jsp?action=wrongpass";
        }
        db.close();
        System.out.println("return_str = " + return_str);
        return return_str;
    }

    public String logout(WrapRequest request) throws Exception{

            HttpSession session = request.getSession(true);

            session.removeAttribute("username");
            session.removeAttribute("password");
            session.removeAttribute("type");

            return  "index.jsp";
    }
}
